<?php
/**
 * Created by JetBrains PhpStorm.
 * User: scala
 * Date: 12-10-28
 * Time: 下午4:50
 * To change this template use File | Settings | File Templates.
 */
class RoleUserAction extends CommonAction
{

    function index(){
        if(!empty($_GET['role_id'])){
            $role=M("Role");
            $data["id"]=$_GET["role_id"];
            $roleObj=$role->where($data)->find();
            $user=M("User");
            $roleUser=M("RoleUser");
            $roleUserList=$roleUser->where("role_id=".$_GET['role_id'])->select();
            $this->assign("role",$roleObj);
            if(is_array($roleUserList)&&count($roleUserList)>0){
                $arrValue=array();
                foreach($roleUserList as $value){
                    $arrValue[]=$value['user_id'];
                }
                $map['id']=array("in",$arrValue);
                $map2["id"]=array("not in",$arrValue);

                //当前用户组下的用户
                $userList=$user->where($map)->select();
                $this->assign("list",$userList);

                //不在当前用户组下的用户
                if(!$userList){
                    $userList2=$user->select();
                    $this->assign("list2",$userList2);
                }else{
                    $userList2=$user->where($map2)->select();
                    $this->assign("list2",$userList2);
                }
            }else{
                $userList2=$user->select();
                $this->assign("list2",$userList2);
            }


            $allRelam=$roleUser->select();
            $this->assign("allRelam",$allRelam);

            $allRole=$role->select();
            $this->assign("allRole",$allRole);

        }
        $this->display();
    }

    function insert(){
        if(!empty($_POST['user_id2'])){
            $userids=$_POST['user_id2'];
            $roleUser=M("RoleUser");
            if(is_array($userids)){
                foreach($userids as $val){
                    $data["role_id"]=$_POST['role_id'];
                    $data["user_id"]=$val;
                    $list=$roleUser->where($data)->select();
                    if($list===null||(is_array($list)&&count($list)==0)){
                        $roleUser->add($data);
                    }
                }
                $this->ajaxReturn($_POST['role_id'],"关联成功！",1);
            }else{
                $this->ajaxReturn("",urlencode("没有选择需要关联的用户！"),2);
            }
        }
    }


    function delete(){
        if(!empty($_POST['user_id'])){
            $userids=$_POST['user_id'];
            $roleUser=M("RoleUser");
            if(is_array($userids)){
                $map['user_id']=array("in",$userids);
                $map['role_id']=array("eq",$_POST['role_id']);
                if($roleUser->where($map)->delete()){
                    $this->ajaxReturn($_POST['role_id'],"删除成功！",1);
                }else{
                    $this->ajaxReturn($_POST["role_id"],"删除失败！",0);
                }
            }
        }else{
            $this->ajaxReturn("",urlencode("没有选择需要移除该组的用户！"),2);
        }
    }




}
